package edu.nf.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

/***
 * 序列化工具类
 */
public class SerializeUtils {

    /**
     * 序列化对象
     */
    private static Gson gson = new Gson();

    /***
     *  序列化
     * @param json
     * @param tClass
     * @return
     * @param <T>
     */
    public static<T> T deserialize(String json,Class<T> tClass) {
        T t = null;
        try {
            t = gson.fromJson(json, tClass);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return t;
    }

    /***
     * 反序列化
     * @param obj 接收对象
     * @return
     * @param <T>
     */
    public static<T> String serialize(T obj){
        String json = "";
        try {
            json = gson.toJson(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return json;
    }
}
